package com.lb.shopguide.ui.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.base.DialogBase;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.util.lb.LbSdcardUtil;


/**
 * author:zhaokang on 2015/8/6 15:06
 * email:clunyes@126.com
 * description:
 */
public class DialogSelectPic extends DialogBase implements View.OnClickListener {
    private TextView tvTitle;
    private LinearLayout layoutTitle;
    private RelativeLayout layout_take_photo;
    private RelativeLayout layout_local_photo;
    private boolean ifCrop;

    private OnSelectPhotoListener mOnSelectPhotoListener;

    public static DialogSelectPic getDialogSelectPic(String text, boolean isCrop) {
        DialogSelectPic dialogChooseGender = new DialogSelectPic();
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.DIALOG_TITLE, text);
        bundle.putBoolean(AppConstant.IFCROP, isCrop);
        dialogChooseGender.setArguments(bundle);
        return dialogChooseGender;
    }

    public static DialogSelectPic getDialogSelectPic(String text) {
        DialogSelectPic dialogChooseGender = new DialogSelectPic();
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.DIALOG_TITLE, text);
        dialogChooseGender.setArguments(bundle);
        return dialogChooseGender;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mFragmentView = inflater.inflate(R.layout.dialog_select_avatar, container, false);
        tvTitle = mFragmentView.findViewById(R.id.tv_dialog_title);
        layoutTitle = mFragmentView.findViewById(R.id.layoutTitle);
        layout_take_photo = mFragmentView.findViewById(R.id.rl_profile_take_photo);
        layout_local_photo = mFragmentView.findViewById(R.id.rl_profile_local_photo);
        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        tvTitle.setText(getArguments().getString(AppConstant.DIALOG_TITLE));
        if (getArguments().containsKey(AppConstant.IFCROP)) {
            ifCrop = getArguments().getBoolean(AppConstant.IFCROP);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.Theme_Base_AppCompat_Dialog_Light_NoTransparent);
    }

    public void initView() {

        layout_take_photo.setOnClickListener(this);
        layout_local_photo.setOnClickListener(this);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public void onClick(View v) {
        if (LbSdcardUtil.getAvailableByte() / 1024 >= 1024) {
            mOnSelectPhotoListener.onTakePhoto(v.getId(), ifCrop);
        } else {
            ToastUtils.showLong("内存空间不足，请清理内存空间");
        }
        DialogSelectPic.this.dismiss();
    }


    public void setOnSelectPhotoListener(OnSelectPhotoListener listener) {
        this.mOnSelectPhotoListener = listener;
    }

    public interface OnSelectPhotoListener {
        void onTakePhoto(int id, boolean ifCrop);
    }

    public interface OnDismissListener {
        void onDismiss();
    }

    private OnDismissListener mOnDismissListener;

    public void setOnDismissListener(OnDismissListener listener) {
        mOnDismissListener = listener;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
